package com.huike.web.controller.sysMenu;

import com.huike.clues.domain.SysMenu;
import com.huike.clues.result.RoleMenuTreeAjaxResult;
import com.huike.clues.service.ISysMenuService;
import com.huike.common.core.domain.TreeSelect;
import com.huike.common.core.domain.entity.SysMenuDTO;
import com.huike.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.util.List;


/**
 * @Description SysMenuController
 * @Author kiloc
 * @Date 2023-10-2023/10/16
 */
@RequestMapping("system/menu")
@Slf4j
@RequiredArgsConstructor
@RestController
@Api(tags = "菜单信息相关接口")
public class SysMenuController {

    private final ISysMenuService iSysMenuService;

    /**
     * 处理获取菜单下拉树列表接口
     *
     * @return
     */
    @ApiOperation("获取菜单下拉树列表")
    @GetMapping("treeselect")
    public Result<List<TreeSelect>> treeselect() {

        log.info("获取菜单下拉树列表接口....");

        List<TreeSelect> treeSelect = iSysMenuService.treeselect();

        return Result.success(treeSelect);
    }

    @ApiModelProperty("加载对应角色菜单列表树接口")
    @GetMapping("roleMenuTreeselect/{roleId}")
    public RoleMenuTreeAjaxResult roleMenuTreeselect(@PathVariable Long roleId) {

        log.info("加载对应角色菜单列表树");

        RoleMenuTreeAjaxResult result = iSysMenuService.roleMenuTreeselect(roleId);

        result.setCode(200);


        return result;
    }

    /**
     * 获取菜单列表
     *
     * @param sysMenuDTO
     * @return
     */
    @ApiOperation("获取菜单列表")
    @GetMapping("list")
    public Result<List<SysMenu>> list(SysMenuDTO sysMenuDTO) {

        log.info("获取菜单列表:{}", sysMenuDTO);

        List<SysMenu> sysMenu = iSysMenuService.findAll(sysMenuDTO);

        return Result.success(sysMenu);
    }

    /**
     * 新增菜单
     *
     * @param sysMenuDTO
     * @return
     */
    @ApiOperation("新增菜单")
    @PostMapping()
    public Result<String> add(@RequestBody SysMenuDTO sysMenuDTO) {

        log.info("新增菜单");

        iSysMenuService.add(sysMenuDTO);

        return Result.success("新增菜单成功");

    }

    /**
     * 根据菜单编号获取详细信息
     *
     * @param menuId
     * @return
     */
    @ApiOperation("根据菜单编号获取详细信息")
    @GetMapping("{menuId}")
    public Result<SysMenu> findByMenuId(@PathVariable Long menuId) {

        log.info("根据菜单编号获取详细信息:{}", menuId);

        SysMenu sysMenu=iSysMenuService.findByMenuId(menuId);

        return Result.success(sysMenu);
    }

    /**
     * 修改菜单
     * @param sysMenuDTO
     * @return
     */
    @ApiOperation("修改菜单")
    @PutMapping()
    public Result<String>update(@RequestBody SysMenuDTO sysMenuDTO){

        log.info("修改菜单");

        iSysMenuService.updateMenu(sysMenuDTO);

        return Result.success("修改菜单");
    }

    /**
     * 删除菜单.存在子菜单或已分配,不允许删除
     * @param menuId
     * @return
     */
    @ApiOperation("删除菜单.存在子菜单或已分配,不允许删除")
    @DeleteMapping("{menuId}")
    public Result<String>delete(@PathVariable Long menuId){

        log.info("删除菜单.存在子菜单或已分配,不允许删除:{}",menuId);

        iSysMenuService.delete(menuId);

        return Result.success("操作成功");
    }
}
